package com.study.socket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/27 19:22
 */
public class Server
{
    public static void main(String[] args) throws IOException
    {
        System.out.println("开始启动服务！！！");
        ServerSocketChannel open = ServerSocketChannel.open();
        open.bind(new InetSocketAddress(9898));
        SocketChannel socketChannel = open.accept();
        FileChannel fileChannel = FileChannel.open(Paths.get("E:\\GoogleDownLoad\\timg2.jpg"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        while (socketChannel.read(byteBuffer) != -1)
        {
            byteBuffer.flip();
            fileChannel.write(byteBuffer);
            byteBuffer.clear();
        }
        fileChannel.close();
        socketChannel.close();
        open.close();
        System.out.println("图片传送结束！！！");
    }
}
